import {
	ref,
	reactive
} from 'vue'
import dayjs from 'dayjs';
import initApi from '@/apis/init.js';
export default function useEquityList() {


	const orderData = reactive({
		params: {
			pageNum: 1,
			pageSize: 20,
			condition: {
				userId: null,
				orderNo: null
			},
			orders: ['updated_time desc']
		},
		loadStatus: 'loading',
		list: [],
		total: 0,
		showPicker: false,
		pickerTitle: null,
		columns: [],
		equtyId: null
	});



	const handleLoadMore = () => {
		console.log('handleLoadMore');
		if (orderData.loadStatus != 'more') {
			return;
		}
		orderData.params.pageNum = orderData.params.pageNum + 1;
		getOrderList();
	}
	const refresh = () => {
		console.log('refresh');
		orderData.params.pageNum = 1;
		getOrderList().finally(() => {
			uni.stopPullDownRefresh();
		});
	}
	const getOrderList = () => {
		console.log('getOrderList');
		// 获取page
		return initApi
			.getReceiveEquityPage(orderData.params)
			.then((res) => {
				console.log(res);
				if (res.code == 20000) {
					orderData.list = orderData.params.pageNum == 1 ? res.data.records : [...orderData.list, ...res.data
						.records
					];
					orderData.loadStatus = res.data.records.length < orderData.params.pageSize ? 'noMore' : 'more';
				}
			})
			.catch((err) => {
				console.log(e, '分页加载 getOrderList');
				orderData.loadStatus = 'more';
				if (orderData.params.pageNum > 1) {
					orderData.params.pageNum = orderData.params.pageNum - 1;
				}
			});
	};
	const getTiket = (item) => {
		orderData.equtyId = item.id;
		// 没有数据 就直接领取
		if (!item.equityPortfolioConfigList || item.equityPortfolioConfigList?.length < 1) {
			confirNoList(item);
			return;
		}
		// 弹框
		if (item.equityType == 1) {
			orderData.pickerTitle = '选择银行立减金';
		} else if (item.equityType == 2) {
			orderData.pickerTitle = '选择虚拟会员';
		}
		orderData.showPicker = true;
		orderData.columns.length = 0;
		orderData.columns.push(item.equityPortfolioConfigList);
	};
	const confirNoList = (item) => {
		initApi
			.getReceiveEquity({
				id: orderData.equtyId,packageCode: item.packageCode, annualPayNum: item.annualPayNum 
			})
			.then((res) => {
				console.log(res);
				if (res.code == 20000) {
					getOrderList();
					return uni.utils.toast('领取成功');
				} else {
					return uni.utils.toast(res.message);
				}
			})
			.catch((error) => {
				return uni.utils.toast(error.message);
			});
	};
	const haveOperate = (item) => {
		/* receiveStatus 领取状态 1 && 当前时间在有效时间范围内 未领取 有操作  
			 receiveStatus 领取状态 1 && 当前时间 早于 生效时间 未到时间  无操作 effectiveTime
			 receiveStatus 领取状态 1 && 当前时间大于过期时间   已过期 无操作 expireTime  */

		const time1 = item.effectiveTime; // 8yue
		const time2 = item.expireTime; // 9yue
		const nowTime = dayjs(new Date()).format('YYYY-MM-DD HH:ss:mm');
		const dayjsTime1 = dayjs(time1);
		const dayjsTime2 = dayjs(time2);
		const dayjsTime3 = dayjs(nowTime);
		if (dayjsTime3.isBefore(dayjsTime1)) {
			return '未到时间';
		} else if (dayjsTime3.isAfter(dayjsTime2)) {
			return '已过期';
		} else if (dayjsTime3.isAfter(dayjsTime1) && dayjsTime3.isBefore(dayjsTime2)) {
			return '未领取';
		} else {
			return '错误时间';
		}
	};
	/* picker */
	const handleClose = () => {
		orderData.showPicker = false
	}

	const confirm = (e) => {
		const {
			value
		} = e;
		console.log(value);
		initApi
			.getReceiveEquity({
				...value[0],
				id: pageParams.equtyId
			})
			.then((res) => {
				console.log(res);
				if (res.code == 20000) {
					getOrderList();
					orderData.showPicker = false;
					return uni.utils.toast('领取成功');
				} else {
					return uni.utils.toast(res.message);
				}
			})
			.catch((error) => {
				return uni.utils.toast(error.message);
			});
	};
	const changeHandler = (e) => {
		console.log(e, 'change');
	};
	return {
		orderData,
		handleLoadMore,
		refresh,
		getOrderList,
		handleClose,
		confirm,
		changeHandler,
		getTiket,
		haveOperate
	}
}